import cv2
import numpy as np
from pyzbar.pyzbar import decode

# frame = cv2.imread("C:\\Users\\86191\Pictures\\Saved Pictures\\Camera Roll\\code.jpg")
datas =['fangyao','chenliao']
cap = cv2.VideoCapture(0)
# 设置分辨率
cap.set(3, 640)
cap.set(4, 480)
# 创建视频写入器
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# vm = cv2.VideoWriter('C:\\Users\\86191\\Videos\\code.avi', fourcc, 20.0, (640, 480))
while True:
    ret,frame = cap.read()
    code = decode(frame)
    for barcode in code:
        # 解码数据
        mydata = barcode.data.decode('utf-8')
        print(mydata)

        # 判断数据是否在列表中
        # if mydata in datas:
        #     mydatas = mydata
        #     colors = (0,255,0)
        #
        # else:
        #     mydatas = 'no is datas'
        #     colors = (0,0,255)
        pts = np.array([barcode.polygon],np.int32)
        pts = pts.reshape((-1,1,2))
        cv2.polylines(frame,[pts],True,(0,0,255),2)
        # 获取二维码位置
        pts2 = barcode.rect
        # 在二维码上写文字
        cv2.putText(frame,mydata,(pts2[0],pts2[1]),cv2.FONT_HERSHEY_SIMPLEX,0.5 ,(0,0,255))

    cv2.imshow('frame',frame)
    # vm.write(frame)
    key = cv2.waitKey(40)
    if key == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()